
// val login= function(){
var app=getApp();
var utils = require("../utils/appUtils.js")

function requestHttp(url,param,success,fail){
  let baseUrl ='https://www.jianxiongxcx.com/?';
  wx.request({
    url: baseUrl+url,
    data:param,
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    method: 'POST',
    success: function (res) {
      success(res)
    },
    fail:function(res){
        fail(res)
    }
  })
}

function requestHttp(url, param, success) {
  let baseUrl = 'https://www.jianxiongxcx.com/?';
  console.log(param)
  param["openid"] = app.globalData.openId;
  param["logincode"] = app.globalData.token;
  console.log(param)
  wx.request({
    url: baseUrl + url,
    data: param,
    header: {
        // 默认值
      'content-type': 'application/x-www-form-urlencoded',
    },
    method: 'POST',
    success: function (res) {
      success(res)
      if (res.data.state == 2 || res.data.msg == "授权失败"||
        res.data.msg=="无用户，请先授权"){
        wx.showModal({
          title: '登录过期',
          content: '您的登录信息已过期,请重新登录!!!',
          showCancel: false,
          confirmText: '确定登录',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击了“返回授权”')
              resetLogin1()
            }
          }
        })
      }
    }
  })
}

function resetLogin1(){
  wx.login({
    success: function (res) {
      console.log(res);
      if (res.code) {
        //参数
        let dataParams = {
          code: res.code
        }
        const value = wx.getStorageSync('userInfo')
        console.log(value.headImage)
        if (value) {
          app.globalData.userNmae = value.nickName;
          app.globalData.avatarUrl = value.headImage;
        }
        //授权后请求获取openid接口
        requestHttp('m=api&c=Index&a=getopenid', dataParams,
          function (res) {
            console.log(res)
            //登录成功设置全局公共参数变量
            app.globalData.openId = res.data.data.openid;
            app.globalData.token = utils.md5(res.data.data.openid + res.data.data.timeout + 'lsj123')
            //保存用户信息到本地
            var saveUserInfo = {};
            saveUserInfo['openId'] = res.data.data.openid;
            saveUserInfo['headImage'] = app.globalData.avatarUrl;
            saveUserInfo['nickName'] = app.globalData.userNmae;
            saveUserInfo['token'] = utils.md5(res.data.data.openid + res.data.data.timeout + 'lsj123')
            console.log(res.data.data.openid);
            wx.setStorageSync('userInfo', saveUserInfo);
            //参数
            var dataParams = {
              openid: app.globalData.openId,
              headimgurl: app.globalData.avatarUrl,
              nickname: app.globalData.userNmae,
              logincode: utils.md5(app.globalData.openId + res.data.data.timeout + 'lsj123')
            }
            //请求登录接口
            requestHttp('m=api&c=Index&a=login', dataParams,
              function (res) {
                console.log(res)
                //请求成功设置全部变量
                // that.onLoad();
              })
          })
      }
    }
  })
}

function resetLogin(investopenid,prizeId,userInfo,loginSuccess) {
  wx.login({
    success: function (res) {
      console.log(res + "===" + investopenid);
      var code=res.code
      if (res.code) {
        let dataParams;
        //参数
        if (investopenid && prizeId){
           dataParams = {
            code: res.code,
            id: prizeId,
            investopenid: investopenid,
            openid:''
          }
        }else{
           dataParams = {
            code: res.code,
            id: prizeId,
            investopenid: investopenid
          }
        }
       
        console.log(res.code)
        //授权后请求获取openid接口
        requestHttp('m=api&c=Index&a=getopenid', dataParams,
          function (res) {
            console.log(res);
            // that.setData({
            //   isshowDialog: true
            // })
            //登录成功设置全局公共参数变量
            app.globalData.openId = res.data.data.openid;
            app.globalData.userNmae = userInfo.nickName;
            app.globalData.avatarUrl = userInfo.avatarUrl;
            app.globalData.token = utils.md5(res.data.data.openid + res.data.data.timeout + 'lsj123')

            //保存用户信息到本地
            var saveUserInfo = {};
            saveUserInfo['openId'] = res.data.data.openid;
            saveUserInfo['headImage'] = userInfo.avatarUrl;
            saveUserInfo['nickName'] = userInfo.nickName;
            saveUserInfo['token'] = utils.md5(res.data.data.openid + res.data.data.timeout + 'lsj123')
            console.log(res.data.data.openid);
            wx.setStorageSync('userInfo', saveUserInfo);
            //参数
            var dataParams = {
              headimgurl: userInfo.avatarUrl,
              nickname: userInfo.nickName,
            }
            //请求登录接口
            requestHttp('m=api&c=Index&a=login', dataParams,
              function (res) {
                //请求成功设置全部变量
                loginSuccess();
                // inviteFriend(code, prizeId, investopenid);
              })
          })
      }
    }
  })
}

function inviteFriend(code, prizeId, investopenid){
  if (investopenid ==0){
    return ;
  }
  console.log(code)
  var dtatParam={
    code: code,
    id: prizeId,
    investopenid: investopenid
  }
  requestHttp("m=api&c=Index&a=getopenid", dtatParam,function(res){
    console.log(res)
  })
}


module.exports = {
  requestHttp: requestHttp,
  resetLogin: resetLogin
}